package base.view;

import base.entity.regitem.DirRegItem;
import base.entity.regitem.RegItem;
import base.factory.impl.RegItemFactory;

public class View {
    protected boolean isFile;
    protected RegItem item;
    protected String path;
    public static final int ONLY_READABLE = 0x01;
    public static final int SYS_FILE = 0x02;
    public static final int READABLE_AND_WRITEABLE = 0x04;
    public static final int NOT_FILE = 0x08;
    public static final int MAX_PROPERTY_NUM = 255;
    // 只读文件
    public boolean isOnlyReadable(){
        int rst = (item.getProperty() & ONLY_READABLE);
        return rst > 0;
    }
    // 是系统文件
    public boolean isSysFile(){
        int rst = (item.getProperty() & SYS_FILE);
        return rst > 0;
    }
    // 可读可写
    public boolean isReadableAndWriteable(){
        int rst = (item.getProperty() & READABLE_AND_WRITEABLE);
        return rst > 0;
    }
    // 不是一个文件项
    public boolean isNotAFile(){
        int rst = (item.getProperty() & NOT_FILE);
        return rst > 0;
    }
    // 获取路径
    public String getPath(){
        return path;
    }
    // 目录
    public RegItem getItem() {
        return item;
    }

    /**
     *
     * @param property 一个在0-255内的整数，需要由 View.ONLY_READABLE、View.SYS_FILE、View.READABLE_AND_WRITEABLE、View.NOT_FILE
     *                 中若干常量进行或运算得到；例如:
     *                 设置登机项属性是 “只读、系统文件” ， 则需要 setProperty(View.ONLY_READABLE | View.SYS_FILE)
     *                 参数超出范围会抛出异常
     * @throws Exception
     */
    public void setProperty(int property) throws Exception {
        if (property > MAX_PROPERTY_NUM){
            throw new Exception("参数property 的值"+property+"超过允许的最大值：" + MAX_PROPERTY_NUM);
        }
        byte p = (byte)property;
        item.setProperty(p);
        System.out.println("设置属性为：" + p + "   ----  " + Integer.valueOf(property).toBinaryString(property));
    }

    public static void main(String[] args) throws Exception {
//        byte a = (byte)4;
//        RegItem regItem = RegItemFactory.getInstance().newDirInstance("aaa",a,a);
//        View view = new DirectoriesView(regItem,"  ");
//        view.setProperty(View.NOT_FILE|View.SYS_FILE);
//        System.out.println("非文件：" + view.isNotAFile());
//        System.out.println("可读写：" + view.isReadableAndWriteable());
//        System.out.println("只读：" + view.isOnlyReadable());
//        System.out.println("系统文件：" + view.isSysFile());

    }
}
